package com.jy.tdata.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.jy.tdata.common.CommonResult;
import com.jy.tdata.entity.AcrelAdwModel;
import com.jy.tdata.entity.BaseMonitor;
import com.jy.tdata.entity.TotalAct;
import com.jy.tdata.mapper.AcrelAdwMapper;
import com.jy.tdata.mapper.AcrelAdwModelMapper;
import com.jy.tdata.mapper.BaseMonitorMapper;
import com.jy.tdata.mapper.TotalActMapper;
import com.jy.tdata.vo.AcreAdwVo;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.beans.PropertyEditorSupport;
import java.util.*;

/**
 * @Author 苏少有
 * @Date 2022/10/12 16:43
 * @Version 1.0
 * @Email 1924626853@qq.com
 * @Description 实时数据累计总有功功率 控制层
 */
@Api(tags = "实时数据累计总有功功率")
@RestController
@RequiredArgsConstructor
@Slf4j
public class TestController {
    private final AcrelAdwMapper acrelAdwMapper;
    private final BaseMonitorMapper baseMonitorMapper;
    private final AcrelAdwModelMapper acrelAdwModelMapper;

    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(DateUtil.parse(text));
            }
        });
    }
    @ApiIgnore
    @GetMapping("/selectTotalActTByTime")
    @Scheduled(fixedRate = 60000)
    public CommonResult selectTotalActTByTime() {
        DateTime startTime = DateUtil.date();
        Integer step = 3;
        List<BaseMonitor> baseMonitorList = baseMonitorMapper.selectByType();
        List<AcreAdwVo> arrayList = new ArrayList<>();
        final DateTime end = startTime.offset(DateField.MINUTE, -step);
        for (BaseMonitor t : baseMonitorList) {
            AcreAdwVo acreAdwVo = acrelAdwMapper.selectActT(t.getMonitorCode());
            if (Objects.isNull(acreAdwVo) || acreAdwVo.getActT() == null) {
                continue;
            }
            Double sum = acreAdwVo.getActT() * t.getCt() * t.getPt();
            Float actT = sum.floatValue();
            if (acreAdwVo.getTs().compareTo(end) < 0) {
                actT = 0f;
            }
            acreAdwVo.setActT(actT);
            arrayList.add(acreAdwVo);
        }
        Double total = arrayList.stream().mapToDouble(AcreAdwVo::getActT).sum();
        System.out.println("total = " + total);
        AcrelAdwModel acrelAdwModel = new AcrelAdwModel();
        acrelAdwModel.setActT(total.floatValue());
        acrelAdwModel.setTs(new DateTime(startTime.offset(DateField.MINUTE, step).getTime()));
        System.out.println("acrelAdwModel = " + acrelAdwModel.getTs()+"-------"+acrelAdwModel.getActT());
        final int insert = acrelAdwModelMapper.insert(acrelAdwModel);
        if (insert < 1) {
            return CommonResult.error("插入实时总有功功率失败!");
        }
        return CommonResult.success("插入实时总有功功率成功");
    }


}
